export default {
	data() {
		return {
			verIndex: '',
			textClass: ''
		}
	},
	methods: {
		// 上一步
		headback() {
			let _this = this;
			_this.list.busiTripRouteEntityList = _this.busiTripRouteEntityList;
			_this.$emit('modifyNum', 0, _this.list)
			return true
		},
		// 下一步
		ngtocost() {
			let _this = this;
			if (_this.verificationData() == true) return false;
			_this.budget();
			_this.list.busiTripRouteEntityList = _this.busiTripRouteEntityList;
			_this.$emit('modifyNum', 2, _this.list)
			return true
		},
		nextStep(btnDef, callback){
			if(this.ngtocost()){
				return callback(3);
			}
			callback("");
		},
		upStep(btnDef, callback){
			if(this.headback()){
				return callback(1);
			}
			callback("");
		},
		validateWorkflowForm(btnDef, callback) {
			if(btnDef.buttonName != "暂存"){
				if(this.wfContext.WorkflowInstinceState == 0 || this.wfContext.WorkflowInstinceState == 1 || this.wfContext.WorkflowInstinceState == 5){
					return callback("请点击下一步填写费用预算");
				}
			}
			callback("");
		},
		//暂存-工作流点击"保存"时触发此函数，返回true:工作流继续提交， false:工作流不提交
		saveWorkflow(btnDef, callback) {
			let _this= this;
			if(_this.travelTrip == "1"){//变更保存
				this.list.billStatus= "0";
				_this.$lyRequest.post("/gateway/reim/mp/dbsap/busiTripmEntity/changeBusiTrip.do", _this.list).then(result=>{
				  if(result.data && result.data.result == 1){
				    callback("", result.data.rows.billNo);
				  }else{
					callback(result.data.msg);
				  }
				});
			} else {
				this.list.billStatus= "0";
				_this.$lyRequest.post("/gateway/reim/mp/dbsap/busiTripmEntity/saveBusiTripm.do", _this.list).then(result=>{
				  if(result.data && result.data.result == 1){
				    callback("", result.data.rows.billNo);
				  }else{
					callback(result.data.msg);
				  }
				});
			}
		},
		//行程校验
		verificationData() {
			let _this = this;
			_this.verIndex = "";
			let verShow = false;
			let busiTripRouteEntityList = _this.busiTripRouteEntityList;
			if(busiTripRouteEntityList.length == 0){
				_this.$api.showTip('请添加行程');
				return true;
			}
			for (let i = 0; i < busiTripRouteEntityList.length; i++) {
				_this.verIndex = i;
				if (busiTripRouteEntityList[i].routeStartDate == '') {
					_this.assignment("routeStartDate", i, "出发日期");
					verShow = true;
					break;
				} else if (busiTripRouteEntityList[i].startLocation == '') {
					_this.assignment("startLocation", i, "出发地点");
					verShow = true;
					break;
				} else if (busiTripRouteEntityList[i].dest == '') {
					_this.assignment("dest", i, "到达地点");
					verShow = true;
					break;
				} 
				// else if (busiTripRouteEntityList[i].descs == '') {
				// 	_this.assignment("descs", i, "备注");
				// 	verShow = true;
				// 	break;
				// }
			}
			return verShow;
		},
		budget() {
			let empNoList = [];
			let removeinequality = [];//删除人员
			let addinequality = [];//增加人员
			if(this.list){
				this.list.busiTripRouteEntityList.forEach((item, index) => {
					empNoList.push({
						empNo: item.empNo,
						empNm: item.empNm,
					})
				});
				empNoList = this.ObjectDeduplication(empNoList);
				this.list.peersNum = empNoList.length - 1;
				this.busiTripdEntityList = [];
				if(!this.list.empNoList) this.list.empNoList= [];
				if(this.list.empNoList.length==0){
					this.list.empNoList = empNoList;
					let _this = this;
					empNoList.forEach((item, index) => {
						_this.list.busiTripdEntityList.push({
							budgetMId: '', //	预算信息主表ID	String	T
							budgetNo: '', //	预算号	String	T
							budgetNm: '', //	预算名称	String	T
							empNm: item.empNm, //	申请人	String	T
							empNo: item.empNo, //	申请人工号	String	T
							subdeptId: _this.list.subdeptId, //	科室编码	String	T
							subdeptNm: _this.list.subdeptNm, //	科室名称	String	T
							totAmt: '', //	申请金额	Double	T	
						})
					})
				}
				this.list.empNoList.forEach((itemA,indexA)=>{
					if(JSON.stringify(empNoList).indexOf(JSON.stringify(itemA)) == -1){
						removeinequality.push(itemA);
					}
				})
				empNoList.forEach((itemA,indexA)=>{
					if(JSON.stringify(this.list.empNoList).indexOf(JSON.stringify(itemA)) == -1){
						addinequality.push(itemA);
					}
				})
				let _this = this;
				let busiTripdEntityList = _this.list.busiTripdEntityList;	
				if(removeinequality.length>0){
					 _this.list.empNoList.map((item, index) => {
						if(JSON.stringify(removeinequality).indexOf(JSON.stringify(item)) !== -1){
							_this.list.busiTripdEntityList.splice(index,1) ;
						}
					})
				}
                if(addinequality.length>0){
					addinequality.map((item, index) => {
							_this.list.busiTripdEntityList.push({
								budgetMId: '', //	预算信息主表ID	String	T
								budgetNo: '', //	预算号	String	T
								budgetNm: '', //	预算名称	String	T
								empNm: item.empNm, //	申请人	String	T
								empNo: item.empNo, //	申请人工号	String	T
								subdeptId: _this.list.subdeptId, //	科室编码	String	T
								subdeptNm: _this.list.subdeptNm, //	科室名称	String	T
								totAmt: '', //	申请金额	Double	T	
							})
					})
				}
			   this.list.empNoList = empNoList;
			   this.list.peersNum = empNoList.length-1;
			}
		},
		ObjectDeduplication(tempArr) {
			for (let i = 0; i < tempArr.length; i++) {
				for (let j = i + 1; j < tempArr.length; j++) {
					if (tempArr[i].empNo == tempArr[j].empNo) {
						tempArr.splice(j, 1);
						j--;
					};
		 	};
			};
			return tempArr;
		},
		assignment(name, i, text) {
			this.textClass = name + i;
			this.commonApi.goAnchor(name + i);
			this.$api.showTip(`行程${i+1}${text}不能为空`);
		}
	}
}
